﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using KCL.Code.Data;

namespace KCL.Code.Business
{
    public class MaterialServices
    {
        public static List<MaterialView> GetAllMaterialViews()
        {
            return (new MaterialDAO()).GetAllMaterialViews();
        }

        public static List<MaterialView> GetMaterialsToReport()
        {
            return (new MaterialDAO()).GetMaterialsToReport();
        }

        public static List<Material> GetAllMaterials()
        {
            return (new MaterialDAO()).GetAllMaterials();
        }

        public static Material GetMaterialByID(string code)
        {
            return (new MaterialDAO()).GetMaterialByID(code);
        }

        public static bool Insert(Material m)
        {
            return (new MaterialDAO()).Insert(m);
        }

        public static bool Update(Material m)
        {
            Material materialToUpdate = GetMaterialByID(m.code);
            if (materialToUpdate != null)
            {
                materialToUpdate.code = m.code;
                materialToUpdate.name = m.name;
                materialToUpdate.productId = m.productId;
                materialToUpdate.colorId = m.colorId;
                materialToUpdate.sizeId = m.sizeId;
                materialToUpdate.thicknessId = m.thicknessId;
                materialToUpdate.madeInId = m.madeInId;
                return (new MaterialDAO()).Update(materialToUpdate);
            }
            return false;
        }

        public static bool Delete(Material m)
        {
            return (new MaterialDAO()).Delete(m);
        }

        public static string GetNextMaterialCode()
        {
            return (new MaterialDAO()).GetNextMaterialCode();
        }

        public static string GenerateSelectHtml(string id)
        {
            List<Material> materials = GetAllMaterials();
            string selectMaterial = "<select id=\\\"comboMaterials\\\">";
            foreach (KCL.Code.Material material in materials)
                selectMaterial += string.Format("<option value=\\\"{0}\\\" {1}>{2}</option>", material.code, id == material.code ? "selected" : "", material.name);
            selectMaterial += "</select>";
            return selectMaterial;
        }
    }
}